package com.wpx.fileSys.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.Box.Filler;

import org.json.JSONArray;
import org.json.JSONObject;

import com.wpx.fileSys.dao.FindDao;
import com.wpx.fileSys.entity.FileList;
import com.wpx.fileSys.utils.JdbcUtils;


public class FindDaoImpl implements FindDao {

	@Override
	public JSONArray searchFiles(String username, String filename) {
		// TODO Auto-generated method stub
		PreparedStatement pstm=null;
		ResultSet rs=null;
		Connection conn=JdbcUtils.getConnection();
		String sql="";
		if (filename.isEmpty()) {
			sql="select file_name,file_time from Files where file_dir=?";
		}else {
			sql="select file_name,file_time from Files where file_dir=? and file_name like ?";
		}
		
		JSONArray jsonArray =new JSONArray();
		
		try {
			pstm=conn.prepareStatement(sql);
			pstm.setString(1, username);
			if (!filename.isEmpty()) {
				pstm.setString(2, "%?%"+filename+"%?%");
			}
			
			rs=pstm.executeQuery();
			while(rs.next()){
				
				String file_name=rs.getString(1);
				String file_time=rs.getString(2);
				
				
				JSONObject jsonObject=new JSONObject();
				jsonObject.put("file_name", file_name);
				jsonObject.put("file_time", file_time);
	
				jsonArray.put(jsonObject);

			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("error 列表查询失败");
		}finally{
			JdbcUtils.close(rs, pstm, conn);
		}
		
		return jsonArray;
	}

	@Override
	public JSONArray searchDatas(String username, String filename) {
		// TODO Auto-generated method stub
//		yys --阴阳师账号
//		yysSR --阴阳师SSR卡
//		yysZH --阴阳师自抽号
//		J --诛仙金币
//		Z --诛仙账号
		FileList fileList=searchfileInfo(username, filename);
		int num=0;
		String sql="";
		switch (fileList.getFile_type()) {
		case "yys":
			sql="select yys_account,yys_pwd,yys_filename "
					+ "from Data_yys where yys_username=? and yys_filename=?";
			num=3;
			break;
		case "yysSR":
			sql="select yysSR_num,yysSR_platform,yysSR_area, yysSR_account,yysSR_pwd,yysSR_SSR,yysSR_amount,yysSR_filename "
					+ "from Data_yysSR where yysSR_username=? and yysSR_filename=?";
			num=7;
			break;
		case "yysZH":
			sql="select yysZH_num,yysZH_area,yysZH_nickname,yysZH_account,yysZH_pwd,yysZH_amount,yysZH_filename "
					+ "from Data_yysZH where yysZH_username=? and yysZH_filename=?";
			num=7;
			break;
		case "j":
			sql="select j_account,j_pwd,j_time,j_coin,j_nickname,j_level,j_area,j_filename "
					+ "from Data_j where j_username=? and j_filename=?";
			num=8;
			break;
		case "z":
			sql="select z_account,z_pwd,z_filename "
					+ "from Data_z where z_username=? and z_filename=?";
			num=3;
			break;

		default:
			System.out.println("不确定的文件类型");
			return null;
		}
		fileList.setFile_sql(sql);
		fileList.setFile_num(num);
		JSONArray resultJson=searchfileData(fileList);
		
		return resultJson;
	}
	
	private static FileList searchfileInfo(String username, String filename) {
		
		PreparedStatement pstm=null;
		ResultSet rs=null;
		Connection conn=JdbcUtils.getConnection();
		String sql="select file_name,file_dir,file_time,file_type "
					+ "from files where file_dir=? and file_name=?";
		FileList fileList=new FileList();
		try {
			pstm=conn.prepareStatement(sql);
			pstm.setString(1, username);
			pstm.setString(2, filename);

			rs=pstm.executeQuery();
			
			while(rs.next()){			
				String file_name=rs.getString(1);
				String file_dir=rs.getString(2);
				String file_time=rs.getString(3);
				String file_type=rs.getString(4);
				
				fileList.setFile_name(file_name);
				fileList.setFile_dir(file_dir);
				fileList.setFile_time(file_time);
				fileList.setFile_type(file_type);
			}
		
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("error 数据查询失败");
		}finally{
			JdbcUtils.close(rs, pstm, conn);
		}
		return fileList;
	}
	
	
	private JSONArray searchfileData(FileList fileList) {
		// TODO Auto-generated method stub
		PreparedStatement pstm=null;
		ResultSet rs=null;
		Connection conn=JdbcUtils.getConnection();
		String sql=fileList.getFile_sql();
		
		JSONArray jsonArray =new JSONArray();
		
		try {
			pstm=conn.prepareStatement(sql);
			pstm.setString(1, fileList.getFile_dir());
			pstm.setString(2, fileList.getFile_name());
			
			rs=pstm.executeQuery();
			while(rs.next()){
				
				JSONArray dataJson=new JSONArray();
				for (int i = 0; i < fileList.getFile_num(); i++) {
					dataJson.put(rs.getString(i+1));
				}
				jsonArray.put(dataJson);	
			}
		
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("error 数据查询失败");
		}finally{
			JdbcUtils.close(rs, pstm, conn);
		}
		
		return jsonArray;
	}




}
